package br.com.future.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SecurityInterceptor extends HandlerInterceptorAdapter {

	private static final Log LOG = LogFactory.getLog(SecurityInterceptor.class);

	/*
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet .http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		if (LOG.isDebugEnabled())
			LOG.debug("SecurityInterceptor - preHandle");

		/*
		 * This is just an example in how we can return an error on from a Interceptor
		 * TODO - Remove this code that is making all requests fails
		 */
		//SecurityContext sc = SecurityContextUtils.getSecurityContextFromRequest(request);
        //sc.setResponse(HttpServletResponse.SC_UNAUTHORIZED, "Incorrect login");

		return true;
	}

	/*
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet .http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		if (LOG.isDebugEnabled())
			LOG.debug("SecurityInterceptor - postHandle");
	}
}
